package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.rms.mapper.CompanyPositionMapper;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.ums.mapper.MemberMapper;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberResult;
import cn.stylefeng.guns.modular.wms.entity.Communicate;
import cn.stylefeng.guns.modular.wms.mapper.*;
import cn.stylefeng.guns.modular.wms.model.params.*;
import cn.stylefeng.guns.modular.wms.model.result.ApplyReadResult;
import cn.stylefeng.guns.modular.wms.model.result.CommunicateResult;
import cn.stylefeng.guns.modular.wms.model.result.JobExpectResult;
import cn.stylefeng.guns.modular.wms.model.result.WorkExpResult;
import  cn.stylefeng.guns.modular.wms.service.CommunicateService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 已沟通记录表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class CommunicateServiceImpl extends ServiceImpl<CommunicateMapper, Communicate> implements CommunicateService {

    @Resource
    private CompanyPositionMapper companyPositionMapper;
    @Resource
    private ResumeMapper resumeMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private WorkExpMapper workExpMapper;
    @Resource
    private ApplyReadMapper applyReadMapper;
    @Resource
    private JobExpectMapper jobExpectMapper;

    @Override
    public void add(CommunicateParam param){
        Communicate entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CommunicateParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CommunicateParam param){
        CommunicateResult result=this.findBySpec(param);
        if (result!=null){
            param.setId(result.getId());
            Communicate oldEntity = getOldEntity(param);
            Communicate newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public CommunicateResult findBySpec(CommunicateParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<CommunicateResult> findListBySpec(CommunicateParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CommunicateParam param){
        Page pageContext = getPageContext();
        IPage<CommunicateResult> page = this.baseMapper.customPageList(pageContext, param);
        if(param.getType()==0){
            page.getRecords().forEach(item -> {
                if (item.getPositionId()!=null){
                    item.setPosition(companyPositionMapper.findBySpec(new CompanyPositionParam().setId(item.getPositionId())));
                }
                //招聘者信息
                MemberResult memberResult=memberMapper.getMember(new MemberParam().setMemberSn(item.getComMemberId()));
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getName());
                item.setMemberPostName(memberResult.getPostName());
                //企业信息
                Company company=companyMapper.selectById(item.getCompanyId());
                if (company!=null){
                    item.setCompanyName(company.getName());
                    item.setCompanyStaffSize(company.getStaffSize());
                    item.setCompanyNature(company.getNature());
                }
            });
        }else{
            page.getRecords().forEach(item -> {
            //求职者信息
            MemberResult memberResult=memberMapper.getMember(new MemberParam().setMemberSn(item.getMemberId()));
            item.setMemberAvatar(memberResult.getAvatar());
            item.setMemberName(memberResult.getName());
            item.setMemberPostName(memberResult.getPostName());
            item.setMemberSex(memberResult.getSex());
            //简历
            item.setResume(resumeMapper.findBySpec(new ResumeParam().setMemberId(memberResult.getMemberId())));
            //工作经历
            List<WorkExpResult> list=workExpMapper.customList(new WorkExpParam().setMemberId(memberResult.getMemberId()));
            if (list.size()>0){
                item.setCompanyName(list.get(0).getCompanyName());
                item.setCompanyPostName(list.get(0).getPostName());
                item.setWorkSkill(list.get(0).getSkill());
            }
            //期望薪资
            List<JobExpectResult> expectResults =jobExpectMapper.customList(new JobExpectParam().setId(memberResult.getMemberId()));
            if (expectResults.size()>0){
                item.setExpectSalary(expectResults.get(0).getExpectSalary());
            }
            });
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(CommunicateParam param) {
        Page pageContext = getPageContext();
        IPage<CommunicateResult> page =this.baseMapper.customPageListForAdmin(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CommunicateParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Communicate getOldEntity(CommunicateParam param) {
        return this.getById(getKey(param));
    }

    private Communicate getEntity(CommunicateParam param) {
        Communicate entity = new Communicate();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
